﻿using AutoMapper;
using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace GlennQuigmire.API.Application.Commands
{
    public class InboundOrderCreateHander : IRequestHandler<InboundOrderCreateCommand, Result>
    {

        private readonly IBaseRepositorys<InboundOrder> inboundorderBaserepository;
        private readonly IBaseRepositorys<InboundOrderProduct> inboundorderProductBaserepository;
        private readonly IBaseRepositorys<Product> productBaserepository;
        private readonly IMapper mapper;

        public InboundOrderCreateHander(IBaseRepositorys<InboundOrder> inboundorderBaserepository, IBaseRepositorys<InboundOrderProduct> inboundorderProductBaserepository, IBaseRepositorys<Product> productBaserepository, IMapper mapper)
        {
            this.inboundorderBaserepository = inboundorderBaserepository;   
            this.inboundorderProductBaserepository = inboundorderProductBaserepository;
            this.productBaserepository = productBaserepository;
            this.mapper = mapper;
        }


        /// <summary>
        /// 添加入库单信息
        /// </summary>
        /// <param name="request">添加入库单请求对象</param>
        /// <param name="cancellationToken">允许取消异步</param>
        /// <returns>添加入库单请求对象响应对象</returns>
        public Task<Result> Handle(InboundOrderCreateCommand request, CancellationToken cancellationToken)
        {
            try 
            {
                var result = new Result();
                InboundOrder inboundOrder = new InboundOrder();
                inboundOrder.InboundOrderNumber = request.InboundOrderNumber;
                inboundOrder.InboundOrderDate = request.InboundOrderDate;
                inboundOrder.InboundOrderTypeId = request.InboundOrderTypeId;
                inboundOrder.CreateId = request.CreateId;
                inboundOrder.Remark = request.Remark;
                inboundOrder.CreateDate = DateTime.Now;
                inboundOrder.CreateName = request.CreateName;
                if (inboundOrder.InboundOrderNumber == "")
                {
                    inboundOrder.InboundOrderNumber = Guid.NewGuid().ToString();
                }
                int n = inboundorderBaserepository.Add(inboundOrder);
                if (n > 0)
                {
                    int inboundOrderId = inboundorderBaserepository.GetAll().FirstOrDefault(c => c.InboundOrderNumber == inboundOrder.InboundOrderNumber).InboundOrderId;
                    foreach (var item in request.InboundOrderItems)
                    {
                        InboundOrderProduct inboundOrderProduct = new InboundOrderProduct();
                        inboundOrderProduct.InboundOrderId = inboundOrderId;
                        inboundOrderProduct.ProductId = item.ProductId;
                        inboundOrderProduct.InboundOrderNum = item.InboundOrderNum;
                        inboundOrderProduct.CreateId = request.CreateId;
                        inboundOrderProduct.CreateDate = DateTime.Now;
                        inboundOrderProduct.CreateName = request.CreateName;
                        int m = inboundorderProductBaserepository.Add(inboundOrderProduct);
                        if (m > 0)
                        {
                            n++;
                            var info = productBaserepository.GetAll().FirstOrDefault(c => c.ProductId == item.ProductId);
                            if (info != null)
                            {
                                info.InventoryStore += item.InboundOrderNum;
                                n+=productBaserepository.Update(info);
                            }
                        }
                    }

                }
                result.Code = n;
                if (n == 1 + (request.InboundOrderItems.Count() * 2))
                {
                    result.Messege = "创建入库单成功";
                }
                else
                {
                    result.Messege = "创建入库单失败";
                }
                return Task.FromResult(result);

            }
            catch (Exception ex)
            {
                throw ex;   
            }
        }
    }
}
